/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.profile.boundary;

import com.aljfce.business.entity.Calendar;
import com.aljfce.business.entity.Event;
import com.aljfce.business.entity.User;
import com.aljfce.business.event.control.EventWrapper;
import com.aljfce.business.profile.control.ProfileManager;
import com.aljfce.business.profile.exception.UserNotFoundException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import javax.inject.Inject;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author EOroan
 */
@ManagedBean(eager = true)
@ViewScoped
public class OtherUserHome {

    @Inject
    ProfileManager pm;

    private ScheduleModel eventModel;
    private Event selectedEvent;
    private User user;
    private String userEmail;

    public String getUserEmail() {
        if (userEmail == null) {
            this.userEmail = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("user");
        }
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public Event getSelectedEvent() {
        return selectedEvent;
    }

    @PostConstruct
    public void init() {

        user = this.getOtherUser();
        if (user != null) {
            this.showOtherUserCalendar();
        }

    }

    /**
     * Get the value of otherUser
     *
     * @return the value of otherUser
     */
    public User getOtherUser() {
        if (user == null) {
            try {
                
                user = pm.getProfile(userEmail);
            } catch (UserNotFoundException | IllegalArgumentException | EJBException ex) {
                //do nothing
            }catch (Exception e){
                //To avoi different ejb exceptions thronw by the container 
            }
        }
        return user;
    }

    /**
     * Set the value of otherUser
     *
     * @param user new value of otherUser
     */
    public void setOtherUser(User user) {
        this.user = user;
    }

    public void setSelectedEvent(Event selectedEvent) {
        this.selectedEvent = selectedEvent;
    }

    public void onEventSelect(SelectEvent selectEvent) {
        EventWrapper ew = (EventWrapper) selectEvent.getObject();
        this.selectedEvent = ew.getEvent();

    }

    public ScheduleModel getEventModel() {
        if (eventModel == null) {
            user = this.getOtherUser();
            if (user != null) {
                this.showOtherUserCalendar();
            }
        }
        return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
        this.eventModel = eventModel;
    }

    public void showOtherUserCalendar() {
        eventModel = new DefaultScheduleModel();
        
        ArrayList<Calendar> calendarList = new ArrayList<>(user.getCalendarCollection());
        Calendar calendar = calendarList.get(0);
        
        for (Event event : calendar.getEventCollection()) {

            EventWrapper ew = new EventWrapper();
            if (event.getVisibility()) {
                ew.setEvent(event);
                eventModel.addEvent(ew);
            } else {
                if (calendar.getVisibility()) {
                    Event ne = new Event();
                    ne.setStartingDate(event.getStartingDate());
                    ne.setEndingTime(event.getEndingTime());
                    ne.setTitle("");
                    ew.setEvent(ne);
                    eventModel.addEvent(ew);

                }
            }

        }

    }

    public void eventDetail(SelectEvent selectEvent) {
        //event = (ScheduleEvent) selectEvent.getObject();
        EventWrapper ew = (EventWrapper) selectEvent.getObject();
        selectedEvent = ew.getEvent();

    }

}
